{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 石头剪刀布\n",
    "- 创建一个命令行游戏，游戏者可以在石头、剪刀和布之间进行选择，与计算机PK。如果游戏者赢了，得分就会添加，直到结束游戏时，最终的分数会展示给游戏者。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "choices = [\"石头\", \"布\", \"剪刀\"]\n",
    "computer = random.choice(choices)\n",
    "player = False\n",
    "cpu_score = 0\n",
    "player_score = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "石头, 布 或者 剪刀?布\n",
      "你输了! 剪刀 胜 布\n",
      "石头, 布 或者 剪刀?剪刀\n",
      "平局!\n",
      "石头, 布 或者 剪刀?剪刀\n",
      "你赢了! 剪刀 胜 布\n",
      "石头, 布 或者 剪刀?石头\n",
      "你输了! 布 胜 石头\n",
      "石头, 布 或者 剪刀?布\n",
      "你赢了! 布 胜 石头\n",
      "石头, 布 或者 剪刀?不\n",
      "这不是一个有效的游戏，检查你的输入!\n",
      "石头, 布 或者 剪刀?石头\n",
      "你赢了! 石头 胜 剪刀\n",
      "石头, 布 或者 剪刀?结束游戏\n",
      "最终比分:\n",
      "电脑得分:2\n",
      "玩家得分:3\n"
     ]
    }
   ],
   "source": [
    "#采用while True循环语句\n",
    "#该语句的核心思想是如果出现错误的话，可以继续循环。\n",
    "while True:\n",
    "    player = input(\"石头, 布 或者 剪刀?\")\n",
    "    # 判断游戏者和电脑的选择\n",
    "    if player == computer:\n",
    "        print(\"平局!\")\n",
    "    elif player == \"石头\":\n",
    "        if computer == \"布\":\n",
    "            print(\"你输了!\", computer, \"胜\", player)\n",
    "            cpu_score+=1\n",
    "        else:\n",
    "            print(\"你赢了!\", player, \"胜\", computer)\n",
    "            player_score+=1\n",
    "    elif player == \"布\":\n",
    "        if computer == \"剪刀\":\n",
    "            print(\"你输了!\", computer, \"胜\", player)\n",
    "            cpu_score+=1\n",
    "        else:\n",
    "            print(\"你赢了!\", player, \"胜\", computer)\n",
    "            player_score+=1\n",
    "    elif player == \"剪刀\":\n",
    "        if computer == \"石头\":\n",
    "            print(\"你输了!\", computer, \"胜\", player)\n",
    "            cpu_score+=1\n",
    "        else:\n",
    "            print(\"你赢了!\", player, \"胜\", computer)\n",
    "            player_score+=1\n",
    "    elif player=='结束游戏':\n",
    "        print(\"最终比分:\")\n",
    "        print(f\"电脑得分:{cpu_score}\")\n",
    "        print(f\"玩家得分:{player_score}\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"这不是一个有效的游戏，检查你的输入!\")\n",
    "    computer = random.choice(choices)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
